package com.atguigu.dga.assessor.storage;

import com.alibaba.druid.sql.visitor.functions.If;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.assessor.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga.meta.bean.TableMetaInfo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Component("TABLE_SIMILAR")
public class TableSimilarAssessor extends Assessor {
    @Override
    public void checkProblem(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws Exception {
        //获取相似度
        String metricParamsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject jsonObject = JSONObject.parseObject(metricParamsJson);
        String days = jsonObject.getString("percent");
        Integer integer = Integer.valueOf(days);

        //获取当前表对象
        TableMetaInfo tableMetaInfo = assessParam.getTableMetaInfo();
        //获取所有表集合对象
        Collection<TableMetaInfo> tableMetaInfos = assessParam.getTableMetaInfoMaps().values();
        //用于储存相似表的集合
        ArrayList<String> strings = new ArrayList<>();

        //循环遍历所有表与当前表比较
        for (TableMetaInfo metaInfo : tableMetaInfos) {

            //判断是否是当前表
            if (tableMetaInfo.getTableName().equals(metaInfo.getTableName())){
                break;
            }
            //判断是否是同层次的表
            if (tableMetaInfo.getTableMetaInfoExtra().getDwLevel().equals(metaInfo.getTableMetaInfoExtra().getDwLevel())){
                break;
            }
            //获取当前表的字段集合
            List<String> correntlList = extractTableColName(tableMetaInfo);
            //获取其余表的字段集合
            List<String> otherlList = extractTableColName(metaInfo);

            //获取集合交集
            Collection intersection = CollectionUtils.intersection(correntlList, otherlList);

            if(intersection.size()>0){
                if (intersection.size()*100/correntlList.size()>integer){
                    //是相似表
                    strings.add(metaInfo.getTableName());
                }
            }

        }
        //处理相似表
        if (strings.size()>0){
            //分数置零
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            //提示
            governanceAssessDetail.setAssessProblem("相似表为:"+strings);
        }


    }

    private List<String> extractTableColName(TableMetaInfo tableMetaInfo) {
        String colNameJson = tableMetaInfo.getColNameJson();
        List<JSONObject> jsonObjects = JSONObject.parseArray(colNameJson, JSONObject.class);
        //用于保存字段的集合
        ArrayList<String> strings = new ArrayList<>();
        for (JSONObject jsonObject : jsonObjects) {
            String name = jsonObject.getString("name");
            strings.add(name);
        }
        return strings;
    }
}
